var AjaxCrud = QualityCheck.AjaxCRUD;
var serverUrl = QualityCheck.GPUD.ServiceConfig;
$(function(){
	var usercode = $("#user-name",parent.parent.document).attr("code");
	var userkey = $("#user-name",parent.parent.document).attr("key");
	var usertype = $("#user-name",parent.parent.document).attr("usertype");
	keyCheck(usercode,usertype,userkey,function(){
		initSmPages();
		window.onresize = function () {
			initSmPages();
		};
		pageBtns();
		getSosNum();
	});
});
function getSosNum(){
	var userKey = $("#user-name",parent.parent.document).attr("key");
	var imei = $(".students-choice-select",parent.document).attr("imei");
	var data = {
			cmd:"getSosphones",
			model:"card",
			userKey:userKey,
			data:{
				imei:imei,
			},
			remark:"",
	};
	AjaxCrud.get(serverUrl.ServerLoginUrl,data,function(res){
		if(res.result == 1 && res.data != undefined && res.data != ""){
			var list = res.data;
			for(var i = 0; i < list.length; i++){
				for(var key in list[i]){
					if(list[i][key] == null || list[i][key] == "null" ){
						list[i][key] = "";
					}
				}
				if($("#sos-number-" + (i+1))){
					$("#sos-number-" + (i+1)).val(list[i].sosphone);
					$("#sos-name-" + (i+1)).val(list[i].remark);
				}
			}
		}else{
			for(var i = 0; i < 3; i++){
				if($("#sos-number-" + (i+1))){
					$("#sos-number-" + (i+1)).val("");
					$("#sos-name-" + (i+1)).val("");
				}
			}
		}
	},null,null);	
}
function addSosList(){
	var imei = $(".students-choice-select",parent.document).attr("imei");
	var list = [];
	for(var i = 0; i < 3; i ++){
//		if($("#sos-number-" + (i+1)).val() != "" || $("#sos-name-" + (i+1)).val() != ""){
			var obj = {};
			obj.imei = imei;
			obj.sosphone = $("#sos-number-" + (i+1)).val();
			obj.remark = encodeURI($("#sos-name-" + (i+1)).val());
			obj.sostype = 1;
			for(var key in obj){
				if(obj[key] == undefined){
					obj[key] = "";
				}
			}
			list.push(obj);
//		}
	}
	if(list.length > 0){
		var userKey = $("#user-name",parent.parent.document).attr("key");
		var data = {
				cmd:"addSosphones",
				model:"card",
				userKey:userKey,
				data:JSON.stringify(list),
				remark:"",
				
		};
		processBegin("正在应用中···");
		AjaxCrud.add(serverUrl.ServerLoginUrl,data,function(res){
			if(res.result == 1 && res.data != undefined && res.data != ""){
				processEnd("设置成功");
				getSosNum();
			}else{
				processError("设置失败");
			}
		},null,null);	
	}
}
function pageBtns(){
	$(".students-choice",parent.document).unbind("click");
	$(".students-choice",parent.document).click(function(){
		$(".students-choice",parent.document).removeClass("students-choice-select");
		$(this).addClass("students-choice-select");
		getSosNum();
	});
	$("#sos-list-set").click(function(){
		addSosList();
	});
}
function initSmPages(){
	var rcH = $(".body-right",parent.document).height() - $(".right-title").height()-3;
	$(".right-content").height(rcH);
	
	var cdsH = $(".right-content").height() - $(".content-title").height() - 40;
	$(".content-data-show").height(cdsH);
}